//
//  XJ_NewCardViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/11.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_NewCardViewController.h"
#import "XJ_ApplayResultViewController.h"
#import "XJ_AlextShowView.h"
#import "XJ_SelectBandView.h"

@interface XJ_NewCardViewController ()
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *rechargeBtn;
@property (weak, nonatomic) IBOutlet UITextField *cardIdTF;
@property (weak, nonatomic) IBOutlet UITextField *bandTF;
@property (weak, nonatomic) IBOutlet UITextField *billDatTF;
@property (weak, nonatomic) IBOutlet UITextField *repayTimeTF;
@property (weak, nonatomic) IBOutlet UITextField *CNV2TF;
@property (weak, nonatomic) IBOutlet UITextField *validityDateTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *chargeTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_scrollView;


@end

@implementation XJ_NewCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)creatMainUI{
    self.nav_titleLabel.text =@"账户充值";
    _getCodeBtn.backgroundColor =Color_ZhuSeDiao;
    _getCodeBtn.layer.cornerRadius =4;
    _getCodeBtn.layer.masksToBounds =YES;
    _rechargeBtn.backgroundColor =Color_ZhuSeDiao;
    _rechargeBtn.layer.cornerRadius =4;
    _rechargeBtn.layer.masksToBounds =YES;
    
    _cardIdTF.keyboardType =UIKeyboardTypeNumberPad;
    _CNV2TF.keyboardType =UIKeyboardTypeNumberPad;
    _phoneTF.keyboardType =UIKeyboardTypeNumberPad;
    _codeTF.keyboardType =UIKeyboardTypeNumberPad;
    _up_scrollView.constant =SafeAreaTopHeight;
    
}
- (IBAction)Click_btn:(id)sender {
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    UIButton *button =(UIButton*)sender;
    switch (button.tag) {
        case 0://发卡银行
        {
            XJ_SelectBandView *alext =[[XJ_SelectBandView alloc]initWithTitle:@"选择" cellType:@"1" array:nil];
            alext.block = ^(id selectValue , id selectValueId) {
                self.bandTF.text =selectValue;
            };
            [alext showXLAlertView];
        }
            break;
        case 1://账单日
        {

            [BRStringPickerView showStringPickerWithTitle:nil dataSource:self.daysArr defaultSelValue:self.daysArr[4] isAutoSelect:NO resultBlock:^(id selectValue) {
                weakSelf.billDatTF.text =selectValue;
            }];

        }
            break;
        case 2://还款日
        {

            
            [BRStringPickerView showStringPickerWithTitle:nil dataSource:self.daysArr defaultSelValue:self.daysArr[4] isAutoSelect:NO resultBlock:^(id selectValue) {
                weakSelf.repayTimeTF.text = selectValue;
                
            }];
        }
            break;
        case 3://CNV2
        {
            XJ_AlextShowView *alext =[[XJ_AlextShowView alloc]initWithImage:@"cvn2_img"];
            [alext showXLAlertView];
        }
            break;
        case 4://有效期
        {
            XJ_AlextShowView *alext =[[XJ_AlextShowView alloc]initWithImage:@"validity_img"];
            [alext showXLAlertView];
        }
            break;
        case 5://获取验证码
        {
            if (_phoneTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"手机号为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            [self loadUserAfterSendVerify];
        }
            break;
        case 6://充值
        {
            if (_cardIdTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"信用卡号为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            if (_bandTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"发卡银行为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            if (_billDatTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"账单日为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            if (_repayTimeTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"还款日为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            if (_CNV2TF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"CVN2为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            if (_validityDateTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"有效期为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_bandTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"发卡银行为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_billDatTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"账单日为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_repayTimeTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"还款日为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_phoneTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"手机号为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            if (_codeTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"验证码为空" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            [self loadMoneyAddCardCharge];
           
        }
            break;
            
        default:
            break;
    }
}

#pragma mark--添加信用卡并充值接口
- (void)loadMoneyAddCardCharge{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    [dic setValue:_cardIdTF.text forKey:@"cardNo"];
    [dic setValue:_CNV2TF.text forKey:@"cvn2"];
    [dic setValue:_validityDateTF.text forKey:@"periodDate"];
    [dic setValue:@"1" forKey:@"bankId"];
    [dic setValue:_billDatTF.text forKey:@"billDate"];
    [dic setValue:_repayTimeTF.text forKey:@"backMoneyDate"];
    [dic setValue:_chargeTF.text forKey:@"chargeCount"];
    [dic setValue:_phoneTF.text forKey:@"mobile"];
    [dic setValue:_codeTF.text forKey:@"verifyCode"];
    
    
    NSLog(@"dic = %@",dic);
 
    
    [XJ_Utils signPost:@"/money/addCardCharge?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        XJ_ApplayResultViewController *result =[[XJ_ApplayResultViewController alloc]init];
        result.resultType =@"1";
        result.ValueStr =self.chargeTF.text;
        [self.navigationController pushViewController:result animated:YES];
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
#pragma mark--获取短信验证码接口
-(void)loadUserAfterSendVerify{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    [dic setValue:_phoneTF.text forKey:@"mobile"];
    [dic setValue:@"6" forKey:@"verifyCodeType"];
    
    [XJ_Utils signPost:@"user/afterSendVerify?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
